
public class utils {

	
	public utils()
	{
		
	}
	/**
	 * Converts from String to byte array MAC address
	 * @param mac
	 * @return
	 */
	public byte[] StringtoMacAddress(String mac)
	{
		byte[] dstMAC;
		int NUM_OF_FIELDS = 6;
		String[] fields = mac.split(":");
		if (fields.length > NUM_OF_FIELDS)
		{
			String[] tmp = new  String[NUM_OF_FIELDS];
			System.arraycopy(fields,0,tmp,0,NUM_OF_FIELDS);
			fields = tmp;
		}
		dstMAC = new byte[NUM_OF_FIELDS];
		for (int i=NUM_OF_FIELDS - fields.length ; i<NUM_OF_FIELDS ; i++)
		{
			dstMAC[i] = Integer.decode("0x"+fields[i-(NUM_OF_FIELDS - fields.length)]).byteValue();
		}
		return dstMAC;
	}
	/**
	 * Converts from String to byte array IP address
	 * @param mac
	 * @return
	 */
	public byte[] StringtoIPAddress(String mac)
	{
		byte[] dstMAC;
		int NUM_OF_FIELDS = 4;
		String[] fields = mac.split("\\.");
		if (fields.length > NUM_OF_FIELDS)
		{
			String[] tmp = new  String[NUM_OF_FIELDS];
			System.arraycopy(fields,0,tmp,0,NUM_OF_FIELDS);
			fields = tmp;
		}
		dstMAC = new byte[NUM_OF_FIELDS];
		for (int i=NUM_OF_FIELDS - fields.length ; i<NUM_OF_FIELDS ; i++)
		{
			dstMAC[i] = Integer.decode(fields[i-(NUM_OF_FIELDS - fields.length)]).byteValue();
		}
		return dstMAC;
	}
	/**
	 * Converts byte[] data packets into Integer string form
	 * @return
	 */
	public String toReadableString(byte[] data)
	{
		String[] tmpArr = new String[data.length*3];
		String concat = "";
		int idx=0;
		for(int i=0 ; i<data.length ; i++)
		{
			if (i > 0 )
			{
				tmpArr[idx++]=" ";
			}
			int num = 0xff & data[i];

			int second1 = (num & 0x0f);
			int first1 = ((num & 0xf0) >> 4);
			
			String second  = Integer.toString(second1); 
			String first  = Integer.toString(first1);
			
			tmpArr[idx++]=first;
			tmpArr[idx++]=second;
			
		}
		for(String con : tmpArr)
		{
			if(con !=null)
			concat = concat + con;
		}
		return concat;
	}
	/**
	 * Converts byte[] data packets into readable Hex form
	 * @return
	 */
	public String toReadbleHex(byte[] data)
	{
		char[] tmpArr = new char[data.length*3];
		int idx=0;
		for(int i=0 ; i<data.length ; i++)
		{
			if (i > 0 )
			{
				tmpArr[idx++]=' ';
			}
			int num = 0xff & data[i];

			int second1 = (num & 0x0f);
			int first1 = ((num & 0xf0) >> 4);
			
			char second  = (char) ((second1<10)?'0'+second1:'A'+second1-10); 
			char first = (char) ((first1<10)?'0'+first1:'A'+first1-10);
			
			tmpArr[idx++]=first;
			tmpArr[idx++]=second;
		}
		return new String(tmpArr,0,idx);
	}
	/** Returns the MAC address.
	 * Converts bytes to String
     * @return MAC address of the source
     */
	public String ConvertoMACAddress(byte[] mac){
		char[] src=new char[17];
		for(int i=0;i<5;i++)
		{
			src[i*3]=hexUpperChar(mac[i]);
			src[i*3+1]=hexLowerChar(mac[i]);
			src[i*3+2]=':';
		}
		src[15]=hexUpperChar(mac[5]);
		src[16]=hexLowerChar(mac[5]);

		return new String(src);
	}
	/** Returns the IP address.
	 * Converts bytes to String
     * @return MAC address of the source
     */
	public String ConvertIPAddress(byte[] src)
      {
           return (src[0] & 0xff) + "." + (src[1] & 0xff) + "." + (src[2] & 0xff) + "." + (src[3] & 0xff);
       }

	
	private char hexUpperChar(byte b){
		b=(byte)((b>>4)&0xf);
		if(b==0) return '0';
		else if(b<10) return (char)('0'+b);
		else return (char)('a'+b-10);
	}

	private char hexLowerChar(byte b){
		b=(byte)(b&0xf);
		if(b==0) return '0';
		else if(b<10) return (char)('0'+b);
		else return (char)('a'+b-10);
	}
}
